package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *
     * 发布评论
     */
    public Integer postCommen(Comment comment) {

        //1.接收的comment对象还没有被评论人的用户id

        //1.1先根据动态id获取动态详情，再在动态详情里获取被评论人的用户id，最后封装到comment里
        ObjectId publishId = comment.getPublishId();
        Movement movement = mongoTemplate.findById(publishId, Movement.class);
        comment.setPublishUserId(movement.getUserId());

        //1.2保存comment到数据库
        mongoTemplate.save(comment);

        //2.更新动态详情表里面的喜欢、评论、点赞等字段的数据，并获取更新后的数据

        //2.1创建Criteria对象，设置查询的条件，根据动态id查询
        Criteria criteria = Criteria.where("id").is(comment.getPublishId());
        Query query = Query.query(criteria);

        //2.2设置要更新的字段
        Update update = new Update();
        //2.3判断当前要处理的评论类型
        if(comment.getCommentType() == CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if(comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else{
            update.inc("loveCount", 1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//获取更新后的数据


        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);

        Integer count = modify.statisCount(comment.getCommentType());
        return count;
    }


    /**
     *
     * 查看评论列表
     */
    public List<Comment> lookComments(String movementId, Integer page, Integer pageSize, int type) {
        //1.创建Criteria对象，设置查询条件，根据动态id和评论类型查询
        //注意，参数类型保持一致,条件有两个
        Criteria criteria =  Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(type);

        //2.根据criteria创建query对象
        Query query = Query.query(criteria);
        query.skip(( page - 1) * pageSize ).limit(pageSize).with(Sort.by(Sort.Order.desc("created")));

        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        return comments;
    }








    /**
     *取消点赞，删除comment
     */
    public Integer delete(Comment comment) {

        //1.删除comment表中的数据
        Criteria criteria = Criteria.where("userId").is(comment.getUserId()).and("publishId").is(comment.getPublishId()).and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Comment.class);


        //2.修改movent表中的点赞、评论、喜欢等数据，并获取更新后的数据

        Criteria criteria1 = Criteria.where("id").is(comment.getPublishId());
        Query query1 =Query.query(criteria1);


        Update update = new Update();
        if(comment.getCommentType() == CommentType.LIKE.getType()){
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount", -1);
        }else{
            update.inc("loveCount", -1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Movement modify = mongoTemplate.findAndModify(query1, update, options, Movement.class);

        Integer count = modify.statisCount(comment.getCommentType());

        return count;
    }


    /**
     *查看comment表是否有数据，取消点赞
     */
    public Boolean isLike(Long userId, String movementId, CommentType commentType) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType.getType());
        Query query = Query.query(criteria);
        boolean result = mongoTemplate.exists(query, Comment.class);

        return result;
    }


//    /**
//     *  判断用户对评论是否已经点过赞
//     * @param userId
//     * @param commentId
//     * @param
//     * @return
//     */
//    public Boolean hasLikeComment(Long userId, String commentId,CommentType commentType) {
//        Criteria criteria = Criteria.where("id").is(new ObjectId(commentId))
//                .and("userId").is(userId)
//                .and("commentType").is(commentType.getType());
//
//        Query query = Query.query(criteria);
//
//        boolean result = mongoTemplate.exists(query, Comment.class);
//        return result;
//    }



   /*
    *//**
     * 把对评论点赞的数据保存到数据库
     * @param comment
     * @return
     *//*
    public Integer saveLikeComment(Comment comment) {

        mongoTemplate.save(comment);

        Criteria criteria = Criteria.where("id").is(comment.getId())
                .and("commentType").is(comment.getCommentType())
                .and("userId").is(comment.getUserId());
        Query query = Query.query(criteria);
        //2.设置要修改的字段
        Update update = new Update();
        update.inc("likeCount",1);
        //3.获取修改后的内容
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Comment c = mongoTemplate.findAndModify(query, update, options, Comment.class);
        Integer likeCount = c.getLikeCount();
        return likeCount;
    }
*/

   /* *//**
     *
     * 取消对评论的点赞
     * @return
     *//*
    public Integer deleteLikeComment(Comment comment) {
        Criteria criteria = Criteria.where("id").is(comment.getId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType());
        Query query = Query.query(criteria);

        Update update = new Update();
        update.inc("likeCount", -1);

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        Comment modify = mongoTemplate.findAndModify(query, update, options, Comment.class);

        Integer likeCount = modify.getLikeCount();
        return likeCount;
    }*/


}
